<?php
class ModelProductInformation extends Model {
	public function addInformation($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "information SET info_type = '" . $data['info_type'] . 
			"', layout_id = '" . (int)$data['layout_id'] . 
			"', status = '" . (int)$data['status'] . "'");
		$information_id = $this->db->getLastId(); 
		
		foreach ($data['information_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . 
				"', language_id = '" . (int)$language_id . 
				"', title = '" . $this->db->escape($value['title']) . 
				"', description = '" . $this->db->escape($value['description']) . "'");
		}
		$this->cache->delete('information');
	}
	
	public function editInformation($information_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "information SET info_type = '" . $data['info_type'] . 
			"', layout_id = '" . (int)$data['layout_id'] . 
			"', status = '" . (int)$data['status'] . "' WHERE information_id = '" . (int)$information_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
		foreach ($data['information_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . 
				"', language_id = '" . (int)$language_id . 
				"', title = '" . $this->db->escape($value['title']) . 
				"', description = '" . $this->db->escape($value['description']) . "'");
		}
		$this->cache->delete('information');
	}

	public function getInformation($information_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information WHERE information_id = '" . (int)$information_id . "'");
		return $query->row;
	}
	
	public function getInformationDescriptions($information_id) {
		$information_description_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
		foreach ($query->rows as $result) {
			$information_description_data[$result['language_id']] = array(
				'information_id' => $result['information_id'],
				'title'          => $result['title'],
				'description'    => $result['description']
			);
		}
		return $information_description_data;
	}
		
	public function getInformations($data = array()) {
		$sql = "SELECT i.*, id.title FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . 
			"information_description id ON i.information_id = id.information_id AND id.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";
		$sql_filter = "";
		if (!empty($data['filter_info_type'])) {
			$sql_filter .= " AND LCASE(i.info_type) LIKE '" . $this->db->escape($data['filter_info_type']) . "%'";
		}
		if (!empty($data['filter_title'])) {
			$sql_filter .= " AND LCASE(id.title) LIKE '" . $this->db->escape($data['filter_title']) . "%'";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
		
		$sort_data = array(
			'i.info_type',
			'id.title'
		);		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY id.title";	
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}		
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		$query = $this->db->query($sql);
		return $query->rows;
	}
		
	public function getTotalInformations($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . 
			"information_description id ON i.information_id = id.information_id AND id.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";
		$sql_filter = "";
		if (!empty($data['filter_info_type'])) {
			$sql_filter .= " AND LCASE(i.info_type) LIKE '" . $this->db->escape($data['filter_info_type']) . "%'";
		}
		if (!empty($data['filter_title'])) {
			$sql_filter .= " AND LCASE(id.title) LIKE '" . $this->db->escape($data['filter_title']) . "%'";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
      	$query = $this->db->query($sql);
		return $query->row['total'];
	}	
}
?>